package logic

import (
	"context"
	"fmt"
	"shopping-mall/models"

	"shopping-mall/shopping_rpc/internal/svc"
	"shopping-mall/shopping_rpc/shopping_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductClassLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddProductClassLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductClassLogic {
	return &AddProductClassLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddProductClassLogic) AddProductClass(in *shopping_rpc.AddProductClassRequest) (*shopping_rpc.AddProductClassResponse, error) {
	// todo: add your logic here and delete this line
	var class models.Class
	if models.DB.Where("name = ?", in.Name).Limit(1).Find(&class).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	if class.ID != 0 {
		return nil, fmt.Errorf("分类已存在")
	}
	class.UserId = int(in.UserId)
	class.Name = in.Name
	if models.DB.Create(&class).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &shopping_rpc.AddProductClassResponse{}, nil
}
